<template>
  <el-dialog :visible="dialogFormVisible" :show-close="false" @close="close">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      :label-width="formLabelWidth"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" autocomplete="off" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择活动区域">
          <el-option
            v-for="item in manager"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" autocomplete="off" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:dialogFormVisible', false)"
        >取 消</el-button
      >
      <el-button type="primary" @click="diaHandle">确 定</el-button>
    </div>

    <div slot="title" class="title">
      <p>{{ isEdit ? '编辑部门' : '添加部门' }}</p>
      <span
        class="el-icon-close"
        @click="$emit('update:dialogFormVisible', false)"
      />
    </div>
  </el-dialog>
</template>
<script>
import { addDepartmentsApi, updateDepartmentsApi } from '@/api/departments'
export default {
  name: '',
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    manager: {
      type: Array,
      default: () => []
    },
    id: {
      type: [Number, String],
      default: ''
    },
    departItem: {
      type: Object
    },
    isEdit: {
      type: Boolean
    }
  },
  data() {
    return {
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      formLabelWidth: '120px',
      rules: {
        code: [{ required: true, message: '请输入部门编码', trigger: 'blur' }],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'blur' }
        ],
        name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  watch: {
    // departItem(newVal, oldVal) {
    //   console.log(newVal, 777888888)
    //   this.form = newVal
    // }
    departItem: {
      handler(newVal, oldVal) {
        console.log(this, 777888888)
        // if (this.isEdit) {
        //   // 编辑
        //   this.form = newVal
        // } else {
        //   this.form = {
        //     code: '',
        //     introduce: '',
        //     manager: '',
        //     name: '',
        //     pid: ''
        //   }
        // }
        this.form = newVal
      },
      immediate: true // 立即执行
    }
  },
  created() {
    // this.getDepartManager()
  },
  methods: {
    close() {
      this.form = {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      }
    },
    // async getDepartManager() {
    //   const res = await getDepartManagerApi()
    //   console.log(res, 7)
    //   this.manager = res.data
    // }
    //  点击对话框中的确认按钮
    diaHandle() {
      // this.$emit('update:dialogFormVisible', false)
      this.$refs.form.validate(async(valid) => {
        if (!valid) return this.$message.error('请输入内容')
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 点击对话框确认按钮===添加
    async doAdd() {
      this.form.pid = this.id
      const res = await addDepartmentsApi(this.form)
      console.log(res, 567)
      //  关闭对话框
      this.$emit('update:dialogFormVisible', false)
      // 成功后触发父级
      this.$emit('success')
    },
    // 点击对话框确认按钮===编辑
    async doEdit() {
      const res = await updateDepartmentsApi(this.form)
      //  关闭对话框
      this.$emit('update:dialogFormVisible', false)
      // 成功后触发父级
      this.$emit('success')
    }
  }
}
</script>
<style lang='scss'  scoped>
.title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
